<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Vue2.x</title>
    <script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>
  </head>
  <body>
    <div id="app">
      父组件:<br />
      {{name}}
      <br />
      <button @click="getName">获取name</button>
      <br />
      {{age}}
      <br />
      <button @click="getAge">获取age</button>
      <hr />
      子组件
      <child />
    </div>
  </body>
  <script>
    const Child = {
      template: `
        <div>
          {{name}}
          <br/>
          <button @click="getName">获取name</button><br/>
          {{age}}
          <br/>
          <button @click="getAge">获取age</button>
        </div>
      `,
      data () {
        return {
          person: {
            name: '李四',
            age: 30
          },
          name: '',
          age: 0
        }
      },
      methods: {
        getName () {
          this.name = this.person.name
        },
        getAge () {
          this.age = this.person.age
        }
      }
    }

    new Vue({
      el: '#app',
      data: {
        person: {
          name: '张三',
          age: 20
        },
        name: '',
        age: 0
      },
      methods: {
        getName () {
          this.name = this.person.name
        },
        getAge () {
          this.age = this.person.age
        }
      },
      components: {
        Child
      }
    })
  </script>
</html>
